package com.shitu.soulemo.a;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.shitu.base.BaseFragment;
import com.shitu.base.net.API;
import com.shitu.base.net.model.Category;
import com.shitu.base.net.model.result.GAllCategory;
import com.shitu.base.net.service.CommonService;
import com.shitu.soulemo.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by majie on 2018/3/20.
 */

public class FenleiFragment extends BaseFragment {
    private TextView fl1;
    private LinearLayout fl1_l1;
    private TextView fl1_1;
    private TextView fl1_2;
    private TextView fl1_3;
    private TextView fl1_4;
    private TextView fl1_5;
    private TextView fl1_6;


    private TextView fl2;
    private LinearLayout fl2_l1;
    private TextView fl2_1;
    private TextView fl2_2;
    private TextView fl2_3;
    private TextView fl2_4;
    private TextView fl2_5;
    private TextView fl2_6;

    private TextView fl3;
    private LinearLayout fl3_l1;
    private TextView fl3_1;
    private TextView fl3_2;
    private TextView fl3_3;
    private TextView fl3_4;
    private TextView fl3_5;
    private TextView fl3_6;

    private ArrayList<List<TextView>> tvList = new ArrayList<>();
    private ArrayList<TextView> titleList = new ArrayList<>();
    private ArrayList<LinearLayout> llList = new ArrayList<>();

    private List<Category> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fenlei_fragment, null);
        fl1 = view.findViewById(R.id.fl1);
        fl1_l1 = view.findViewById(R.id.fl1_line1);
        fl1_1 = view.findViewById(R.id.fl1_tv1);
        fl1_2 = view.findViewById(R.id.fl1_tv2);
        fl1_3 = view.findViewById(R.id.fl1_tv3);
        fl1_4 = view.findViewById(R.id.fl1_tv4);
        fl1_5 = view.findViewById(R.id.fl1_tv5);
        fl1_6 = view.findViewById(R.id.fl1_tv6);

        fl2 = view.findViewById(R.id.fl2);
        fl2_l1 = view.findViewById(R.id.fl2_line1);
        fl2_1 = view.findViewById(R.id.fl2_tv1);
        fl2_2 = view.findViewById(R.id.fl2_tv2);
        fl2_3 = view.findViewById(R.id.fl2_tv3);
        fl2_4 = view.findViewById(R.id.fl2_tv4);
        fl2_5 = view.findViewById(R.id.fl2_tv5);
        fl2_6 = view.findViewById(R.id.fl2_tv6);

        fl3 = view.findViewById(R.id.fl3);
        fl3_l1 = view.findViewById(R.id.fl3_line1);
        fl3_1 = view.findViewById(R.id.fl3_tv1);
        fl3_2 = view.findViewById(R.id.fl3_tv2);
        fl3_3 = view.findViewById(R.id.fl3_tv3);
        fl3_4 = view.findViewById(R.id.fl3_tv4);
        fl3_5 = view.findViewById(R.id.fl3_tv5);
        fl3_6 = view.findViewById(R.id.fl3_tv6);

        titleList.clear();
        titleList.add(fl1);
        titleList.add(fl2);
        titleList.add(fl3);

        List<TextView> list1 = new ArrayList<>();
        list1.add(fl1_1);
        list1.add(fl1_2);
        list1.add(fl1_3);
        list1.add(fl1_4);
        list1.add(fl1_5);
        list1.add(fl1_6);

        List<TextView> list2 = new ArrayList<>();
        list2.add(fl2_1);
        list2.add(fl2_2);
        list2.add(fl2_3);
        list2.add(fl2_4);
        list2.add(fl2_5);
        list2.add(fl2_6);

        List<TextView> list3 = new ArrayList<>();
        list3.add(fl3_1);
        list3.add(fl3_2);
        list3.add(fl3_3);
        list3.add(fl3_4);
        list3.add(fl3_5);
        list3.add(fl3_6);

        tvList.clear();
        tvList.add(list1);
        tvList.add(list2);
        tvList.add(list3);

        llList.clear();
        llList.add(fl1_l1);
        llList.add(fl2_l1);
        llList.add(fl3_l1);

        initViews();
        return view;
    }

    private void initViews() {
        API.send(CommonService.getAllCategorys(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("tag", "fail fenlei");
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                try {
                    String str = response.body().string();
                    GAllCategory data = JSON.parseObject(str, GAllCategory.class);
                    list = data.getData();
                    if (getActivity() != null) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                refreshUi();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void refreshUi() {
        for (int i = 0; i < list.size(); i++) {
            titleList.get(i).setText(list.get(i).getName());
            int index = list.get(i).getSubCates().size();
            if (index < 3) {
                llList.get(i).setVisibility(View.GONE);
            }
            for (int j = 0; j < 6; j++) {
                if (j >= index) {
                    tvList.get(i).get(j).setVisibility(View.INVISIBLE);
                } else {
                    tvList.get(i).get(j).setVisibility(View.VISIBLE);
                    tvList.get(i).get(j).setText(list.get(i).getSubCates().get(j).getName());
                }
            }
        }
    }
}
